##################################################################
# Main analysis file for the JOP paper.
##################################################################
#set working directory
setwd("~/Dropbox/Gender and Judicial Elections/JOP replication files")
#clear working environment
rm(list=ls())
#load packages
library(tidyverse) 
library(ggplot2)
library(stargazer)
library(readstata13)
library(splitstackshape)
library(lubridate)
library(lfe)
library(ggpubr)
library(xtable)
library(openxlsx)
library(gghighlight)
library(modelsummary)
library(ggpubr)

########################################################################
# Ads Analysis
########################################################################
ads <- read.xlsx("./WMP_data.xlsx") %>% mutate(creative=str_trim(creative)) %>%
  full_join(.,read.xlsx("./WMP_coding.xlsx") %>%
              mutate(creative=str_trim(creative))) %>%
  mutate(across(starts_with("trait_code"),
                ~dplyr::recode(., `1`="bold",`2`="caring",`3`="compassionate",`4`="competent",
                               `5`="experienced",`6`="fatherlymotherly",`8`="fighter",`9`="hardworker",
                               `10`="honesty",`11`="independent",`12`="innovative",`13`="protect",
                               `14`="proven",`15`="selfmade",`16`="tough",`17`="courage",`18`="stoodupto",
                               `19`="trust",`20`="leader",`21`="careerpol",`22`="corrupt",
                               `23`="dangerous",`24`="dishonest",`25`="hypocrite",`26`="incompetent",
                               `27`="negative",`28`="reckless",`29`="softweak",`31`="insider",`32`="strong",
                               `33`="compromising",`34`="reform",`35`="aggressive",`36`="justice",
                               `37`="fairness",`38`="secondchance",`39`="victim",`40`="safe",
                               `41`="othercourts",`42`="policeendorse",`43`="prosecutor")
  )) %>%
  unite("trait", c(trait_code1:trait_code8,trait_code9), na.rm = TRUE, remove = FALSE,sep=",") %>%
  splitstackshape::cSplit_e(., "trait", sep = ',', fill = 0, type = "character") %>%
  full_join(.,read.xlsx("./candidate_level_clean.xlsx") %>%
              dplyr::select(-category),by="cand_id") %>%
    group_by(cand_id) %>%
  fill(gender_char, .direction = "downup") %>% ungroup() %>% distinct() %>%
  mutate(toughtotal=trait_tough+trait_fighter+trait_prosecutor+trait_policeendorse+trait_experienced+trait_protect+trait_strong,
         anytough = ifelse(toughtotal!=0,1,0),
         reformtotal=trait_reform+trait_compassionate+trait_victim+trait_secondchance,
         anyreform = ifelse(reformtotal!=0,1,0),
         tone=ifelse(tone_num==0,NA,ifelse(tone_num==1,"contrast",ifelse(tone_num==2,"negative","positive"))),
         multiple_winners=ifelse(is.na(multiple_winners),0,1),
         vote_per=vote_per*100,gender=ifelse(gender_char=="female",1,0)) 

#######################################
# Table 2: tough appeals
#######################################
r1 <- felm(trait_tough~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r2 <- felm(trait_fighter~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r3 <- felm(trait_prosecutor~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r4 <- felm(trait_policeendorse~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r5 <- felm(trait_experienced~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r6 <- felm(trait_protect~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r7 <- felm(trait_strong~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

stargazer(r1,r2,r3,r4,r5,r6,r7,title="Gender and Tough-on-Crime Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day, state,","affiliate, race category, and election type fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Tough","Fight","Prosecutor","Police Endorse","Experienced","Protect","Strong"),label = "toughappeals",
          out = "./tables/Table2.tex",notes.align = "l",font.size="tiny",column.sep.width = "-10pt")

#######################################
# Table 3: reform appeals
#######################################
r1 <- felm(trait_reform~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r2 <- felm(trait_compassionate~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r3 <- felm(trait_victim~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)
r4 <- felm(trait_secondchance~as.factor(gender)+log(1+est_cost)+demmention+repmention+won+number_candidates+multiple_winners+vote_per+incumbent+former_prosecutor|tone+daypart+programtype+affiliate+state+category+election_type_clean|0|0,data=ads)

stargazer(r1,r2,r3,r4,title="Gender and Reform Campaign Appeals in 2017 and 2018 Judicial Elections, Unique Ads Only",
          notes=c("Results from an OLS regression. Ad tone, program type, part of day,","affiliate, race category, and election type fixed effects included."),
          covariate.labels = c("Female","Logged Ad Cost","Democrat Mention","Republican Mention","Candidate Won","Number of Candidates","Multiple Winners","Vote Percentage","Incumbent","Former Prosecutor"),
          dep.var.labels = c("Reform","Compassionate","Victim","Second Chance"),label = "rehabappeals",
          out = "./tables/Table3.tex",notes.align = "l",font.size="tiny",column.sep.width = "-7pt") 

########################################################################
# Survey Experiment Analysis
########################################################################
# Import raw data: 
dat <- read.xlsx("./experiment_qualtricsdata.xlsx") %>% 
  slice(3:n()) %>%
  filter(Q3.1=="1,3") %>% filter(Q1.1!="") #or "Red,Green"
# filter to only those that passed the attention check and those that provided affirmative consent 

marybaileyrehab <- dat %>% filter(Q4.3!="") %>%
  mutate(candidate="female",platform="rehab",treatment="FR") %>%
  dplyr::select(ResponseId:Q4.14,Q13.1:treatment) %>% #Q4.3:Q4.14
  rename_all(.,funs(str_replace(.,"Q4.","question"))) 
michaelbaileyrehab <- dat %>% filter(Q5.3!="") %>%
  mutate(candidate="male",platform="rehab",treatment="MR") %>%
  dplyr::select(ResponseId:Q3.1,Q5.3:Q5.14,Q13.1:treatment) %>%  #Q5.3:Q5.14
  rename_all(.,funs(str_replace(.,"Q5.","question"))) 
michaelbaileytough <- dat %>% filter(Q6.3!="") %>%
  mutate(candidate="male",platform="tough",treatment="MT")  %>%
  dplyr::select(ResponseId:Q3.1,Q6.3:Q6.14,Q13.1:treatment) %>% #Q6.3:Q6.14
  rename_all(.,funs(str_replace(.,"Q6.","question")))  
marybaileytough <- dat %>% filter(Q7.3!="") %>%
  mutate(candidate="female",platform="tough",treatment="FT")  %>%
  dplyr::select(ResponseId:Q3.1,Q7.3:Q7.14,Q13.1:treatment) %>% #Q7.3:Q7.14
  rename_all(.,funs(str_replace(.,"Q7.","question")))  

clean <- rbind(marybaileyrehab,michaelbaileyrehab,michaelbaileytough,marybaileytough) %>%
  mutate_at(vars(question6:question14,Q13.1), as.numeric) %>% 
  mutate(question6 = dplyr::recode(question6, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question7 = dplyr::recode(question7, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question8 = dplyr::recode(question8, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_1 = dplyr::recode(question12_1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_2 = dplyr::recode(question12_2, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_3 = dplyr::recode(question12_3, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question13 = dplyr::recode(question13, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Q13.1 = dplyr::recode(Q13.1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Treatment=ifelse(treatment=="FR","Female Reform",
                          ifelse(treatment=="MR","Male Reform",
                                 ifelse(treatment=="FT","Female Tough","Male Tough"))),
         Gender=ifelse(candidate=="female","Female","Male"),
         Platform=ifelse(platform=="rehab","Reform","Tough")) 
#recode to very unlikely - very likely

#######################################
# Figure 1
#######################################
my_comparisons <- list(c("Female","Male"))
ggplot(clean, aes(x=as.factor(Gender), y=question6)) + 
  geom_violin() + stat_summary(fun.y=mean, geom="point", shape=23, size=2) + theme_bw()+
  stat_summary(fun = mean, geom = "text", size=6,
               vjust = -0.65, aes(label = paste("Mean:", round(..y.., digits = 2))))+xlab("Gender")+
  stat_compare_means(method="t.test",size=6,label = "p.format",comparisons = my_comparisons)+
  ylab("Likelihood of Voting for Candidate")+scale_y_continuous(breaks=c(1,2,3,4),
                                                                labels=c("Very\n Unlikely","Somewhat\n Unlikely","Somewhat\n Likely", "Very\n Likely"))
ggsave("./figures/Figure1.pdf",height=8,width=12)

#######################################
# Figure 2
#######################################
my_comparisons <- list(c("Female Tough","Male Tough"),c("Female Reform","Male Reform"))

ggplot(clean, aes(x=as.factor(Treatment), y=question6)) + 
  geom_violin() + stat_summary(fun.y=mean, geom="point", shape=23, size=2) + theme_bw()+
  stat_summary(fun = mean, geom = "text", size=6,
               vjust = -0.65, aes(label = paste("Mean:", round(..y.., digits = 2))))+xlab("Treatment")+
  ylab("Likelihood of Voting for Candidate")+ stat_compare_means(method="t.test",size=6,comparisons = my_comparisons)+
  scale_y_continuous(breaks=c(1,2,3,4),labels=c("Very\n Unlikely","Somewhat\n Unlikely","Somewhat\n Likely", "Very\n Likely"))
ggsave("./figures/Figure2.pdf",height=8,width=12)

